using UnityEngine;

//定义一个结构体，用于存储游戏数据
public struct GameData
{
    public int coin;//金币数量
    public int score;//分数
    public string Time;//通关时间
}
public class GameRoot : MonoBehaviour
{
    //制作单例模式
    public static GameRoot Instance;
    void Awake()
    {
        Instance = this;
    }
    
    
    public HandleLobbyWindow LobbyWindow;
    
    public HandleTipsWindow TipsWindow;
    
    public HandleFightWindow FightWindow;

    private GameData gameData {get; set;}

    // Start is called before the first frame update
    void Start()
    {
        //初始化游戏数据
        InitGameData();
        OpenLobbyWindow();//显示大厅界面
        
        OpenTipsWindow("欢迎来到游戏！");
        OpenTipsWindow("请点击屏幕开始游戏！");
        
        // LobbyWindow.RefreshDataInfo();
    }

    private void InitGameData()
    {
        GameData data = new GameData();
        //是否首次登录
        if (PlayerPrefs.HasKey("isFirstLogin"))
        {
            //获取存储的数据
            data.coin = PlayerPrefs.GetInt("coin");
            data.score = PlayerPrefs.GetInt("score");
            data.Time = PlayerPrefs.GetString("Time");
            gameData = data;
        }
        else
        {
            //初始化数据
            data.coin = 8888;
            data.score = 0;
            data.Time = "00:00:00";
            gameData = data;
            //存储数据
            PlayerPrefs.SetInt("coin", gameData.coin);
            PlayerPrefs.SetInt("score", gameData.score);
            PlayerPrefs.SetString("Time", gameData.Time);   
            OpenTipsWindow("首次登录，赠送8888金币！");
        }
    }
    
    private void OpenLobbyWindow()
    {
        LobbyWindow.gameObject.SetActive(true);
        LobbyWindow.Init();
    }
    
    public void OpenFightWindow()
    {
        LobbyWindow.gameObject.SetActive(false);//关闭大厅界面
        FightWindow.gameObject.SetActive(true); //打开战斗界面
        FightWindow.Init(); //初始化战斗界面
    }
    
    public int GetCoin()
    {
        return gameData.coin;
    }
    
    public int GetScore()
    {
        return gameData.score;
    }
    
    public string GetTime()
    {
        return gameData.Time;
    }
    
    //tips显示
    public void OpenTipsWindow(string tip)
    {
        TipsWindow.AddTip(tip);
    }
    
}
